package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.InviteBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.LoginInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.MyInvitePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IMyInviteView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.MyInviteAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/9.
 * Desc   :
 */

public class MyInviteActivity extends BaseActivity<MyInvitePresenter> implements IMyInviteView {
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.recyclerview_myinvite)
    RecyclerView recyclerviewMyinvite;
    @BindView(R.id.tv_invite_immediately)
    TextView tvInviteImmediately;

    private MyInviteAdapter myInviteAdapter;
    private String token;
    private String u_id;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_my_invite);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new MyInvitePresenter(this);
    }

    @Override
    protected void init() {
        LoginInfoBean loginInfo = SharedPreferencesUtil.getsInstances(this).getLoginInfo();
        token = loginInfo.getToken();
        u_id = loginInfo.getU_id();
        setTopView();
        recyclerviewMyinvite.setLayoutManager(new LinearLayoutManager(mContext));
        mPresenter.getInviteList(token);
    }

    private void setTopView() {
        imgLeft.setImageResource(R.mipmap.icon_close_black);
        txtTitle.setText("我的邀请");
    }

    @OnClick({R.id.ll_leftview, R.id.tv_invite_immediately})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.tv_invite_immediately:
                final String url = "http://gxcm.quan-oo.com/index/users/translate?uid=" + u_id;
                OnekeyShare oks = new OnekeyShare();
                //关闭sso授权
                oks.disableSSOWhenAuthorize();
                // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
                oks.setTitle(getString(R.string.app_name));
                // titleUrl是标题的网络链接，QQ和QQ空间等使用
                oks.setTitleUrl(url);
                // url仅在微信（包括好友和朋友圈）中使用
                oks.setUrl(url);
                oks.setShareContentCustomizeCallback((platform, paramsToShare) -> {
                    paramsToShare.setText(getString(R.string.app_name));
                    paramsToShare.setText("我分享了一个动态");
                    if (Wechat.NAME.equals(platform.getName())) {
                        paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                        paramsToShare.setTitleUrl(url);
                        paramsToShare.setTitle(getString(R.string.app_name));
                    } else if (QQ.NAME.equals(platform.getName())) {
                        paramsToShare.setTitleUrl(url);
                        paramsToShare.setTitle(getString(R.string.app_name));
                    } else if (SinaWeibo.NAME.equals(platform.getName())) {
                        paramsToShare.setTitleUrl(url);
                        paramsToShare.setTitle(getString(R.string.app_name));
                    }
                });
                // 启动分享GUI
                oks.show(this);
                break;
        }
    }

    @Override
    public void setInviteList(InviteBean data) {
        List<InviteBean.ListBean> list = new ArrayList<>();
        InviteBean.ListBean user = data.getUser();
        user.setUser(true);
        list.add(user);
        for (InviteBean.ListBean bean : data.getList()) {
            bean.setUser(false);
            list.add(bean);
        }
        myInviteAdapter = new MyInviteAdapter(this, list);
        recyclerviewMyinvite.setAdapter(myInviteAdapter);
    }
}
